package edu.washington.cs.sounddetector.server.database;

import java.util.Collection;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Queries;
import javax.jdo.annotations.Query;
import javax.jdo.annotations.Serialized;

/**
 * Database table for saving sounds and sound samples for training the classifier.
 * 
 * @author the.dylan.price@gmail.com
 * 
 */
@PersistenceCapable
@Queries({ @Query(
        name = "QUERY_BY_USER_DETECTOR",
        language = "JDOQL",
        value = "SELECT FROM edu.washington.cs.sounddetector.server.database.Sound WHERE userId == :userId && soundClass.detectorId == :detectorId") })
public class Sound
{
    /**
     * Named Query.
     * 
     * @param userId
     *            (String)
     * @param detectorId
     *            (String)
     */
    public static final String QUERY_BY_USER_DETECTOR = "QUERY_BY_USER_DETECTOR";

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.NATIVE)
    private long id;
    private String userId;
    private DbSoundClass soundClass;
    @Serialized
    private byte[] sound;
    @Serialized
    private double[] soundSamples;
    @Serialized
    private double[] features;

    public Sound(DbSoundClass soundClass, byte[] sound, double[] soundSamples, double[] features)
    {
        // TODO: change this when we implement users
        this.userId = "1";
        this.soundClass = soundClass;
        this.sound = sound;
        this.soundSamples = soundSamples;
        this.features = features;
    }

    /**
     * @return the sampleId
     */
    public long getId()
    {
        return id;
    }

    /**
     * @return the userId
     */
    public String getUserId()
    {
        return userId;
    }

    /**
     * @param userId
     *            the userId to set
     */
    public void setUserId(String userId)
    {
        this.userId = userId;
    }

    /**
     * @return the soundClass
     */
    public DbSoundClass getSoundClass()
    {
        return soundClass;
    }

    /**
     * @param soundClass
     *            the soundClass to set
     */
    public void setSoundClass(DbSoundClass soundClass)
    {
        this.soundClass = soundClass;
    }

    /**
     * @return the sound
     */
    public byte[] getSound()
    {
        return sound;
    }

    /**
     * @param sound
     *            the sound to set
     */
    public void setSound(byte[] sound)
    {
        this.sound = sound;
    }

    /**
     * @return the soundSamples
     */
    public double[] getSoundSamples()
    {
        return soundSamples;
    }

    /**
     * @param soundSamples
     *            the soundSamples to set
     */
    public void setSoundSamples(double[] soundSamples)
    {
        this.soundSamples = soundSamples;
    }

    /**
     * @return the features
     */
    public double[] getFeatures()
    {
        return features;
    }

    /**
     * @param features
     *            the features to set
     */
    public void setFeatures(double[] features)
    {
        this.features = features;
    }

    /**
     * Convenience method for executing {@link #QUERY_BY_USER_DETECTOR}.
     */
    public static Collection<Sound> query(PersistenceManager pm, String userId, String detectorId)
    {
        javax.jdo.Query query = pm.newNamedQuery(Sound.class, QUERY_BY_USER_DETECTOR);
        @SuppressWarnings("unchecked")
        Collection<Sound> results = (Collection<Sound>) query.execute(userId, detectorId);
        return results;
    }

}
